﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using SoucreGame.GLO;


namespace SoucreGame.GUI
{
    class guiDatTen
    {
        
        Texture2D guiManHinhNen; // hinh anh lam nen
        Texture2D guiDatten;
        Texture2D cmdGhiTen;
        Texture2D cmdDongY;
        Texture2D cmdThoat;
        SpriteFont FontDatTen;

        Dictionary<string, Rectangle> dsToaDoGiaoDien;
        ContentManager content;
        GameTime gameTime;
        SpriteBatch spriteBatch;

        string strName;
        Vector2 VT2;

        public guiDatTen(ContentManager _content, GameTime _gameTime, SpriteBatch _spritBatch)
        {
            content = _content;
            gameTime = _gameTime;
            spriteBatch = _spritBatch;

            if (content != null)
            {
                guiManHinhNen = content.Load<Texture2D>("GUII\\guiManHinhNen");
                guiDatten = content.Load<Texture2D>("GUII\\guiDatTen");
                cmdGhiTen = content.Load<Texture2D>("GUII\\cmdGhiTen");
                cmdDongY = content.Load<Texture2D>("GUII\\cmdDongY");
                cmdThoat = content.Load<Texture2D>("GUII\\cmdThoat");
                FontDatTen = content.Load<SpriteFont>("SpriteFont");
                strName = "_";
                VT2 = new Vector2(548,342);
               
                dsToaDoGiaoDien = content.Load<Dictionary<string, Rectangle>>("GUII\\guiViTri");
            }
        }

        public void Update(GameTime gameTime)
        {
            CGlobalvariable.trangthaitruoc = CGlobalvariable.trangthaihientai;
            CGlobalvariable.trangthaihientai = Mouse.GetState();
            if (CGlobalvariable.trangthaihientai.LeftButton == ButtonState.Pressed && CGlobalvariable.trangthaitruoc.LeftButton == ButtonState.Released)
            {
                if (dsToaDoGiaoDien["cmdDongY"].Contains(new Point(CGlobalvariable.trangthaihientai.X, CGlobalvariable.trangthaihientai.Y)))
                {
                    CGlobalvariable.Chuyengiaodien = true;
                    CGlobalvariable.Giaodiendangchon = GUIEnum.guiMapGamePlay;
                }
                else if (dsToaDoGiaoDien["cmdThoat"].Contains(new Point(CGlobalvariable.trangthaihientai.X, CGlobalvariable.trangthaihientai.Y)))
                {
                    CGlobalvariable.Chuyengiaodien = true;
                    CGlobalvariable.Giaodiendangchon = GUIEnum.guiLevel;
                }
            }
            CGlobalvariable.KeyTruoc = CGlobalvariable.KeyHienTai;
            CGlobalvariable.KeyHienTai = Keyboard.GetState();
            foreach (Keys key in CGlobalvariable.KeyHienTai.GetPressedKeys())
            {
                if (CGlobalvariable.KeyTruoc.IsKeyUp(key))
                {
                    if (key == Keys.Back && strName.Length > 0)
                    {
                        strName = strName.Remove(strName.Length - 2, 2);
                        strName += "_";
                    }
                    else
                    {
                        strName = strName.Remove(strName.Length - 1, 1);
                        strName += key.ToString() + "_";
                    }

                }
            }
        }

        public void Draw(GameTime gameTime)
        {
            this.spriteBatch.Draw(guiManHinhNen, Vector2.Zero, Color.White);
            this.spriteBatch.Draw(guiDatten, new Vector2(dsToaDoGiaoDien["guiDatTen"].X, dsToaDoGiaoDien["guiDatTen"].Y), Color.White);
            this.spriteBatch.Draw(cmdGhiTen, new Vector2(dsToaDoGiaoDien["cmdGhiTen"].X, dsToaDoGiaoDien["cmdGhiTen"].Y), Color.White);
            this.spriteBatch.Draw(cmdDongY, new Vector2(dsToaDoGiaoDien["cmdDongY"].X, dsToaDoGiaoDien["cmdDongY"].Y), Color.White);
            this.spriteBatch.Draw(cmdThoat, new Vector2(dsToaDoGiaoDien["cmdThoat"].X, dsToaDoGiaoDien["cmdThoat"].Y), Color.White);

            this.spriteBatch.DrawString(FontDatTen,strName,VT2,Color.Black);
        }
    }
}
